package com.oasistudio.tk.connector.imp;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 淘客店铺搜索可以获得店铺名称
 * 通过店铺名称和店铺搜索，可以得到店铺的宝贝搜索地址：
 * http://shop.m.taobao.com/shop/shop_auction_search.htm?suid=63369331&vm=clt&sid=465e77b5506bf285a30dc19db207beb4&ttid=b0a054
 * 从上述URL获取到的信息中，包含搜索店铺宝贝的链接：
 * http://shop.m.taobao.com/shop/a-5-15-42-43789071.htm?vm=clt
 * &sid=465e77b5506bf285a30dc19db207beb4
 * &ttid=400000_12448590%40%E6%B7%98%E9%87%91%E8%B1%86_Android_1.0.0_91
 * 访问这个链接，可以得到宝贝列表和另外三个搜索宝贝的链接
 * 掌柜推荐：
 * "bid":"http://shop.m.taobao.com/shop/a-3-bid-5-15-34-1-42-43789071.htm?vm=clt&sid=465e77b5506bf285a30dc19db207beb4&ttid=400000_12448590%40%E6%B7%98%E9%87%91%E8%B1%86_Android_1.0.0_91", 
 * 新品上架：
 * "newStarts":"http://shop.m.taobao.com/shop/a-3-oldstarts-5-15-34-1-42-43789071.htm?vm=clt&sid=465e77b5506bf285a30dc19db207beb4&ttid=400000_12448590%40%E6%B7%98%E9%87%91%E8%B1%86_Android_1.0.0_91", 
 * 热卖排行：
 * "hotsell":"http://shop.m.taobao.com/shop/a-3-hotsell-5-15-34-1-42-43789071.htm?vm=clt&sid=465e77b5506bf285a30dc19db207beb4&ttid=400000_12448590%40%E6%B7%98%E9%87%91%E8%B1%86_Android_1.0.0_91", 
 */

public class TaobaoShopAuctionsSearch{
	public static final int TIMEOUT = 10000;
	private String url;
//	private int currentPage = 1;//页码
//	private int pageSize = 15;//每页记录数

	public TaobaoShopAuctionsSearch(){
	}
	
	public void setUrl(String url){
		this.url = url;
	}
//	public void setCurrentPage(int currentPage){
//		this.currentPage = currentPage;
//	}
//	public void setPageSize(int pageSize){
//		this.pageSize = pageSize;
//	}
	
	public AuctionsInfo syncConnect() {
		System.out.println("mellon:TaobaoShopAuctionsSearch.syncConnect() url = "+url);
		try {
			long startTime = System.currentTimeMillis();
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet();
			request.setURI(new URI(url));
			HttpResponse response = client.execute(request);
			ByteArrayOutputStream bs = new ByteArrayOutputStream();
			int i = 0;
			DataInputStream dis = new DataInputStream(response.getEntity().getContent());
			byte[] b = new byte[2048];
			while ((i = dis.read(b, 0, 2048)) != -1) {
				bs.write(b, 0, i);
				if (System.currentTimeMillis() - startTime > TIMEOUT * 4) {
					bs.reset();
					break;
				}
			}
			return this.syncPaser(bs.toByteArray());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public AuctionsInfo syncPaser(byte[] all) {
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMinimumFractionDigits(2);
		nf.setMaximumFractionDigits(2);
		AuctionsInfo auctionsInfo = new AuctionsInfo();
		try {
			String str = new String(all, "UTF-8");
			JSONObject jso = new JSONObject(str);
			if (jso.has("itemsArray")) {
				auctionsInfo.isSuccess = true;
				auctionsInfo.shopId = jso.optLong("shopId");
				auctionsInfo.shopTitle = jso.optString("shopTitle");
				auctionsInfo.totalResults = jso.optInt("totalResults");
				auctionsInfo.totalPage = jso.optInt("totalPage");
				auctionsInfo.currentPage = jso.optInt("currentPage");
				auctionsInfo.pageSize = jso.optInt("pageSize");
				auctionsInfo.nextPageUrl = jso.optString("nextPageUrl");
				JSONArray array = jso.getJSONArray("itemsArray");
				for (int i = 0; i < array.length(); i++) {
					JSONObject auctionJSON = array.getJSONObject(i);
					Auction auction = new Auction();
					auction.title = auctionJSON.optString("title");
					auction.picUrl = auctionJSON.optString("picUrl");
					auction.auctionUrl = auctionJSON.optString("auctionUrl");
					auction.sold = auctionJSON.optInt("sold");
					auction.money = auctionJSON.optString("money");
					auction.auctionType = auctionJSON.optString("auctionType");
					auction.hdfk = auctionJSON.optBoolean("hdfk");
					auctionsInfo.auctions.add(auction);
				}
			} else {
				auctionsInfo.isSuccess = false;
				//shopsInfo.errCode = response.errCode;
				//shopsInfo.errInfo = response.errInfo;
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return auctionsInfo;
	}

	public static class AuctionsInfo {
		public String errCode;
		public String errInfo;
		public Boolean isSuccess;
		public Number shopId;
		public String shopTitle;
		public Number totalResults;
		public String bid;//bid url
		public String newStarts;//新品上架
		public String hotsell;//热销商品
		public Number currentPage;
		public Number totalPage;
		public Number pageSize;
		public String nextPageUrl;
		public List<Auction> auctions = new ArrayList<Auction>();
	}
	public static class Auction{
		public String title;
		public String picUrl;
		public String auctionUrl;
		public Number sold;
		public String money;
		public String auctionType;
		public Boolean hdfk;
		
		public String toString(){
			StringBuilder sb = new StringBuilder();
			sb.append("title:"+title);
			sb.append(";money:"+money);
			sb.append(";auctionType:"+auctionType);
			return sb.toString();
		}
	}
}



